<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card>
      <el-input placeholder="请输入内容" class="input-with-select" style="width:410px" v-model="queryInfo.query" @blur="getGoodsList">
        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
      </el-input>
      <el-button type="primary" @click="goAdd">添加商品</el-button>

      <!-- 表格组件 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="280"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="180"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scoped">{{scoped.row.add_time|dataFormat}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 8, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品列表的参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      //   商品列表的数据
      goodsList: [],
      total: 0,
      serach:""
    };
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品列表失败");
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      console.log(res);
      console.log(this.total);
    },
    // 一页有多少条数据
    handleSizeChange(size) {
      this.queryInfo.pagesize = size;
      this.getGoodsList();
    },
    // 页码
    handleCurrentChange(num) {
      this.queryInfo.pagenum = num;
      this.getGoodsList();
    },
    // 添加功能
    goAdd() {
      this.$router.push("/goods/add");
    },
    // 删除商品
    async deleteGoods(id) {
      const result = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(error => {
        return "clac";
      });
      console.log(result);
      if (result === "clac") {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      }
      if (result == "confirm") {
        const { data: res } = await this.$http.delete(`goods/${id}`);
        if (res.meta.status !== 200) {
          return this.$message.error("删除失败");
        }
        this.$message.success("删除成功");
        this.getGoodsList();
      }
    },
  },
  created() {
    this.getGoodsList();
  }
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 20px;
}

.el-table {
  margin-top: 20px;
}
</style>
